//---------------------------------------------------------------------------

#include <vcl.h>
#pragma hdrstop

#include "Invoices.h"
#include "InvoiceDlg.h"
#include "FormUtilities.h"
#include "InvoiceReport.h"

//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
//---------------------------------------------------------------------------
__fastcall TfrmInvoices::TfrmInvoices(TComponent* Owner, TDatabase * dbMain)
        : TForm(Owner)
{
   TFormUtilities::LocateForm(( TForm*)Owner, this);

   this->dbMain = dbMain; 
   invoices = new TStringList();
   invoices->Duplicates = dupAccept;
   bridge = new TInvoiceBridge( dbMain);
}

void TfrmInvoices::SetButtonsState( int row)
{
   TInvoice * invoice;
   bool enabled;

   if ( invoices->Count > 0)
   {
      invoice = ( TInvoice*)invoices->Objects[ row - 1];
      enabled = ( !invoice->isCanceled);
   }
   else
   {
      enabled = false;
   }//if-else

   btnUpdate->Enabled = enabled;
   btnDelete->Enabled = ( invoices->Count > 0);
   btnCancel->Enabled = enabled;
}

void TfrmInvoices::RefreshGrid()
{
   TInvoice* invoice;
   int rowCount;
   int i;

   rowCount = 2;
   bridge->GetInvoices( invoices);

   if ( invoices->Count > 1)
      rowCount = invoices->Count + 1;

   grdInvoices->RowCount = rowCount;

   for ( i = 1; i < grdInvoices->RowCount; i++)
      grdInvoices->Rows[ i]->Clear();

   for ( i = 0; i < invoices->Count; i++)
   {
      invoice = ( TInvoice*)invoices->Objects[ i];

      grdInvoices->Cells[ 0][ i + 1] = DateToStr( invoice->date);
      grdInvoices->Cells[ 1][ i + 1] = invoice->page;
      grdInvoices->Cells[ 2][ i + 1] = invoice->invoiceID;
      grdInvoices->Cells[ 3][ i + 1] = FloatToStr( invoice->amount) + " " + CurrencyString;
      grdInvoices->Cells[ 4][ i + 1] = invoice->paymentType->description;

      if ( invoice->isCanceled)
         grdInvoices->Cells[ 5][ i + 1] = "Cancelled";
      else
         grdInvoices->Cells[ 5][ i + 1] = "OK";
   }//for

   TFormUtilities::SetLastColumnWidth( grdInvoices);

   SetButtonsState( grdInvoices->Row);
}


//---------------------------------------------------------------------------

void __fastcall TfrmInvoices::FormShow(TObject *Sender)
{
   grdInvoices->Cells[ 0][ 0] = "DATE";
   grdInvoices->Cells[ 1][ 0] = "PAGE";
   grdInvoices->Cells[ 2][ 0] = "INVOICE";
   grdInvoices->Cells[ 3][ 0] = "AMOUNT";
   grdInvoices->Cells[ 4][ 0] = "PAYMENT TYPE";
   grdInvoices->Cells[ 5][ 0] = "STATUS";

   RefreshGrid();
}
//---------------------------------------------------------------------------


void __fastcall TfrmInvoices::btnAddClick(TObject *Sender)
{
   TfrmInvoiceDlg * frmInvoiceDlg;

   frmInvoiceDlg = new TfrmInvoiceDlg( this, dbMain, bridge, NULL);

   try
   {
      if ( frmInvoiceDlg->ShowModal() == mrOk)
         RefreshGrid();
   }
   __finally
   {
      delete frmInvoiceDlg;
   }
}
//---------------------------------------------------------------------------

void __fastcall TfrmInvoices::FormDestroy(TObject *Sender)
{
   int i;
   
   delete bridge;

   for( i = 0; i < invoices->Count; i++)
      delete invoices->Objects[ i];
   invoices->Clear();
   delete invoices;
}
//---------------------------------------------------------------------------

void __fastcall TfrmInvoices::btnDeleteClick(TObject *Sender)
{
   TInvoice* original;
   bool gapExists;
   bool allowed;

   original = ( TInvoice*)invoices->Objects[ grdInvoices->Row - 1];

   gapExists = TfrmInvoiceDlg :: IsGapExist( bridge, original, NULL);

   if ( gapExists)
   {
      allowed = ( MessageDlg( "Gap in invoice numbers will be created!\nAre you sure?",
                              mtWarning,
                              TMsgDlgButtons() << mbYes << mbNo,
                              0) == mrYes);
   }
   else
   {
      allowed = ( MessageDlg( "Are you sure?",
                              mtWarning,
                              TMsgDlgButtons() << mbYes << mbNo,
                              0) == mrYes);
   }//if-else

   if ( allowed)
   {
      bridge->Delete( original->id);
      delete original;
      invoices->Delete( grdInvoices->Row - 1);
      RefreshGrid();
   }//if      
}
//---------------------------------------------------------------------------

void __fastcall TfrmInvoices::grdInvoicesSelectCell(TObject *Sender,
      int ACol, int ARow, bool &CanSelect)
{
   SetButtonsState( ARow);        
}
//---------------------------------------------------------------------------

void __fastcall TfrmInvoices::btnCancelClick(TObject *Sender)
{
   TInvoice* invoice;
   
   if ( MessageDlg( "Canceling invoice is irreversible operation!\nAre you sure?",
                    mtWarning,
                    TMsgDlgButtons() << mbYes << mbNo,
                    0) == mrYes)
   {
      invoice = ( TInvoice*)invoices->Objects[ grdInvoices->Row - 1];
      bridge->Cancel( invoice->id);
      RefreshGrid();
   }//if                 
}
//---------------------------------------------------------------------------

void __fastcall TfrmInvoices::btnUpdateClick(TObject *Sender)
{
   TfrmInvoiceDlg * frmInvoiceDlg;
   TInvoice * invoice;

   invoice = ( TInvoice*)invoices->Objects[ grdInvoices->Row - 1];        
   frmInvoiceDlg = new TfrmInvoiceDlg( this, dbMain, bridge, invoice);

   try
   {
      if ( frmInvoiceDlg->ShowModal() == mrOk)
         RefreshGrid();
   }
   __finally
   {
      delete frmInvoiceDlg;
   }
}
//---------------------------------------------------------------------------

void __fastcall TfrmInvoices::grdInvoicesKeyDown(TObject *Sender,
      WORD &Key, TShiftState Shift)
{
   switch ( Key)
   {
      case VK_INSERT :
         btnAddClick( Sender);
         break;

      case VK_DELETE :
         if ( btnDelete->Enabled)
            btnDeleteClick( Sender);
         break;

      case VK_SPACE :
         if ( btnUpdate->Enabled)
            btnUpdateClick( Sender);
         break;
   }
}
//---------------------------------------------------------------------------

void __fastcall TfrmInvoices::btnCloseClick(TObject *Sender)
{
   Close();        
}
//---------------------------------------------------------------------------



